package com.example.springbook.Controller;

import com.example.springbook.Model.UserInfo;
import com.example.springbook.Service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public String login(String userName, String password, HttpSession session){
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return "用户名或密码为空";
        }
        UserInfo userInfo = userService.getUserInfoByName(userName);
        if(userInfo == null){
            return "用户名字为空";
        }
        if(!password.equals(userInfo.getPassword())){
            return "密码错误";
        }
        session.setAttribute("user_session_key",userInfo);
        return "";
    }
}

